package com.ias.agdis_r.fragment.task;

import android.content.Intent;
import android.os.Bundle;
//import android.support.v4.app.Fragment;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.ias.agdis_r.R;
import com.ias.agdis_r.activity.task.NFTaskInfoActivity;
import com.ias.agdis_r.adapter.NFTaskListViewAdapter;
import com.ias.agdis_r.manager.DataManager;
import com.ias.agdis_r.module.MessageModel;
import com.ias.agdis_r.util.TaskUtil;


/**
 * Created by spoon on 2017/3/20.
 */
public class NFTaskListFragment extends Fragment {

    private int sortType = TaskUtil.SORT_TIME_ASC;
    private ListView mListView;// 列表
    private NFTaskListViewAdapter mAdapter;// 列表的适配器
    private View view;

    public void updateUI(String tag) {
        // TODO Auto-generated method stub
        // 接收刷新信息，刷新页面
        if (tag.equals(MessageModel.ITSD) || tag.equals(MessageModel.TTSD)
                || tag.equals(MessageModel.REFRESH)||tag.equals(MessageModel.TRPT)) {
            checkInfo();
            sort(this.sortType);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.task_nflight_tab_01, container, false);
        checkInfo();
        setListAdapter();
        return view;
    }

    private void checkInfo() {
        try {
            if (null != view) {
                TextView noInfo = (TextView) view.findViewById(R.id.tv_noInfo);
                DataManager dm = DataManager.getInstance();
                if (0 == dm.getNFWorkTaskNum() && 0 == dm.getNFReceiveTaskNum()) {
                    noInfo.setVisibility(View.VISIBLE);
                } else {
                    noInfo.setVisibility(View.GONE);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setListAdapter() {
        mListView = (ListView) view.findViewById(R.id.ListView);
        mAdapter = new NFTaskListViewAdapter(getActivity()
                .getApplicationContext(), false, false);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {

                Intent intent = new Intent(getActivity()
                        .getApplicationContext(), NFTaskInfoActivity.class);
                intent.putExtra(MessageModel.TNB, mAdapter.getTNB(arg2));
                startActivity(intent);
            }
        });
    }

    /**
     * 增加排序功能
     *
     * @param sortType
     */
    public void sort(int sortType) {
        this.sortType = sortType;
        if (mAdapter != null) {
            mAdapter.setSortType(sortType);
            //ADAPTER排序
            mAdapter.notifyDataSetChanged();
        }
    }
}
